﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using VersionComparer.Views;

namespace VersionComparer.DataModel.Actions
{
    [ActionAttribute("Execute file",34, System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Enter)]
    public class ExecuteFileAction : SpecificAction
    {
        public ExecuteFileAction()
            : base()
        {
        }

        public override bool RefreshAfterFinish
        {
            get
            {
                return false;
            }
            set
            {
            }
        }
        public override bool MayBeExecuted(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            if (focusedSelectedItems.Length == 0)
                return false;
            ComparableItem item = focusedSelectedItems.Last();
            if (!item.Text.StartsWith("http:", StringComparison.CurrentCultureIgnoreCase))
            {
                if (File.Exists(item.Text))
                    return true;
            }
            return false;
        }

        public override System.Drawing.Image Image(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
             if (focusedSelectedItems.Length == 0)
                return null;
            ComparableItem item = focusedSelectedItems.Last();
            if (!item.Text.StartsWith("http:", StringComparison.CurrentCultureIgnoreCase))
            {
                if (File.Exists(item.Text))
                    return Icon.ExtractAssociatedIcon(item.Text).ToBitmap();;
            }
            return null;
        }
        
        protected override void ExecuteAction(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            var item = focusedSelectedItems.Last();
            Process process = new Process();
            process.StartInfo = new ProcessStartInfo("\"" + item.Text + "\"");
            process.Start();
        }
    }
}
